//
//  ParametersVIew.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/19/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit

final class GestureWithTag: UITapGestureRecognizer {
    var tag: Int!
}

final class PinchWithTag: UIPinchGestureRecognizer {
    var tag: Int!
}


protocol ParametersViewDelegate {
    func didSelectParameter (item: ParametersViewModel)
    func didClearValue (value: String?, placeholder: String, itemIndex: Int)
}

final class ParametersView: UIStackView {

    private var parameters: [ParameterItemProtocol] = []
    var delegate: ParametersViewDelegate!
    
    var items: [ParametersViewModel] = [] {
        didSet {
            if (parameters.count != 0) {
                for view in parameters {
                    view.container.removeFromSuperview()
                }
                parameters = []
                self.addViews()
            } else {
                self.addViews()
            }
        }
    }
    
    // MARK:- Actions
    
    @objc func parameterSelected(sender: GestureWithTag) {
        delegate.didSelectParameter(item: items[sender.tag - 1])
    }
    
    // MARK:- Adding subviews (parameters as views)
    
    private func addViews () {
       
        for (i, item) in items.enumerated() {
            
            let parameterItem = ParameterItem(view: self)
            parameterItem.delegate = self
            parameterItem.itemIndex = i
            parameterItem.setUpView(value: item.value,
                                           icon: item.icon,
                                           placeholder: item.placeholder)
            
            let tapGesture = GestureWithTag(target: self, action: #selector(parameterSelected(sender:)))
            tapGesture.tag = i + 1
            parameterItem.container.addGestureRecognizer(tapGesture)
            
            if (i != 0) {
                let _ = Divider(parent: parameterItem.container, color: .greyBlue, inset: 16)
            }
            
            parameters.append(parameterItem)
        }
    }
    
    // MARK:- Initialization
    
    init () {
        super.init(frame: .zero)
        axis = .vertical
        distribution = .fillProportionally
        alignment = .fill
        customStackView(radius: 10.0)
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - Methods

extension ParametersView {
    func setBackground (color: UIColor) {
        for parameter in parameters {
            parameter.container.backgroundColor = color
        }
    }
    func setCorner (_ radius: CGFloat) {
        for parameter in parameters {
            parameter.container.layer.cornerRadius = radius
        }
    }
}


extension ParametersView: ParameterItemDelegate {
    
    func removeEmptyView (index: Int) {}
    
    func didRemoveValue(value: String?, placeholder: String, itemIndex: Int) {
        self.removeEmptyView(index: itemIndex)
        delegate.didClearValue(value: value, placeholder: placeholder, itemIndex: itemIndex)
    }
}
